package com.mgq.juc.design.countdown;

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author MaGuangQi
 * 模拟5v5游戏加载,加载完成后,游戏开始
 * @date 2021-12-23 16:22
 **/
public class GameCountDownLatch {

    public static void main(String[] args) throws InterruptedException {
        String[] all = new String[10];
        Random random = new Random();
        AtomicInteger num = new AtomicInteger(0);
        ExecutorService executorService = Executors.newFixedThreadPool(10, r -> new Thread(r,"t"+num.getAndIncrement()));
        CountDownLatch latch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            int k = i;
            executorService.submit(()->{
                for (int j = 0; j < 100; j++) {
                    all[k] = Thread.currentThread().getName()+"("+(j + 1) + "%)";
                    try {
                        Thread.sleep(random.nextInt(100));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.print("\r" + Arrays.toString(all));
                }
                latch.countDown();
            });
        }
        latch.await();
        System.out.println("\n游戏开始");
    }
}
